<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Carbon\Carbon;
use App\Models\ParkInfo;
use App\Models\ParkRecord;
use App\Models\Report;

class FlowCountDay extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'flow:count_day';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '流量统计';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $m = 6;
        ini_set('memory_limit', '250M');
        for($i=1; $i<=30;$i++) {
            if($i<10) {
                $start = strtotime("2018-0$m-0" . $i . " 00:00:00");
                if (($i+1) < 10){
                    $end = strtotime("2018-0$m-0" . ($i+1) . " 00:00:00");
                } else {
                    $end = strtotime("2018-0$m-" . ($i+1) . " 00:00:00");
                }
            } else {
                $start = strtotime("2018-0$m-" . $i . " 00:00:00");
                if (($i+1) <= 30){
                    $end = strtotime("2018-0$m-" . ($i+1) . " 00:00:00");
                } else {
                    $end = strtotime("2018-07-" . ($i+1) . " 00:00:00");
                }
            }

            $parks = ParkInfo::whereIn('park_code', config('custom.report_parks'))
                ->select(['park_code', 'parking_name'])
//            ->where('park_code', '7100000226')
                ->orderBy('sort_by', 'desc')
                ->get();

            $data = $parks->map(function ($item, $key) use ($start, $end) {
                //生成报表列
                $rowKey = 'time_' . $item->park_code;
                $row = [
                    $rowKey . '_0_15_free' => 0,
                    $rowKey . '_15_30_free' => 0,
                    $rowKey . '_0_15_charge' => 0,
                    $rowKey . '_15_30_charge' => 0,
                    $rowKey . '_free' => 0,
                    $rowKey . '_charge' => 0,
                    $rowKey . '_total' => 0,
                    $rowKey . '_greater_1440_free' => 0,
                    $rowKey . '_greater_1440_charge' => 0
                ];

                for ($i = 30; $i < 1440; $i += 30) {
                    $endTIme = $i + 30;
                    $row[$rowKey . "_{$i}_{$endTIme}_free"] = 0;
                    $row[$rowKey . "_{$i}_{$endTIme}_charge"] = 0;
                }


                //查询前一天数据
                $table = 't_park_record_' . $item->park_code;
                $parkRecord = new ParkRecord();
                $parkRecord->setTable($table);
                $res = $parkRecord
                    ->where('out_time', '>=', $start)
                    ->where('out_time', '<', $end)
                    ->select(['parking_duration', 'parking_charge'])->get();

                foreach ($res as $value) {
                    //定位时间范围
                    $filed = $this->initFiled($value, $rowKey);
                    //收费车或免费车
                    if ($value->parking_charge < 1) {
                        $row[$rowKey . '_free'] += 1;
                        if (isset($row[$filed . '_free'])) {
                            $row[$filed . '_free'] += 1;
                        } else {
                            '';
                        }
                    } else {
                        if (isset($row[$filed . '_charge'])) {
                            $row[$filed . '_charge'] += 1;
                        } else {
                            '';
                        }
                        $row[$rowKey . '_charge'] += 1;
                    }

                    $row[$rowKey . '_total'] += 1;
                }
                return $row;
            });
            //存入数据库
            $report = new Report();
            $report->report_date = date("Y-m-d",$start);
            $report->type = 'flow_count_day_report';
            $report->value = $data->collapse();
            $report->save();
        }
        $this->info('流量统计成功');
    }

    /**
     * 计算时间范围
     *
     * @param $value
     * @param $key
     * @return string
     */
    private function initFiled($value, $key)
    {
        $time = ceil($value->parking_duration / 60);
        if ($time > 0 && $time <= 15) return "{$key}_0_15";
        if ($time > 15 && $time <= 30) return "{$key}_15_30";

        for ($i = 30; $i < 1440; $i += 30) {
            if ($time > $i && $time <= ($i + 30)) return "{$key}_" . $i . "_" . ($i + 30);
        }
        if($time>1440) return $key . "_greater_1440";
    }
}
